﻿using Engine;
using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System;
using Game.IContentReader;
using System.Text;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Web;
using static Game.Server.Plugins.CreatureDropZHPlugin;
using Newtonsoft.Json.Linq;
using static Game.TerrainContentsGenerator21;

namespace Game.Server.Plugins
{
    public class CreatureDropZHPlugin : ServerPlugin, ICreatureHealthEventHandle, IPlayerEnterGameEventHandle
    {

        public class BlockDropItem 
        {
            public int I;
            public int C;
            public float P;
        }

        public override int Version => 10000;

        public override string Name => "动物自定义掉落";

        public byte FirstLevel => 0;

        public static string ConfigFilePath => Storage.GetSystemPath(ServerManager.PluginsPath + "/CreatureDrops.json");

        public SubsystemPickables subsystemPickables;

        public static Dictionary<string, List<BlockDropItem>> CreatureDropItems = new Dictionary<string, List<BlockDropItem>>();

        public SubsystemGameInfo subsystemGameInfo;

        public override void Initialize()
        {
            CreatureHealthEventManager.AddObject(this);
            PlayerEnterGameEventManager.AddObject(this);
        }

        public override void Load()
        {
            if (File.Exists(ConfigFilePath))
            {
                using (StreamReader file = File.OpenText(ConfigFilePath))
                {
                    string jsonText = file.ReadToEnd();
                    CreatureDropItems = JsonConvert.DeserializeObject<Dictionary<string, List<BlockDropItem>>>(jsonText);
                }
            }
        }

        public override void Save()
        {
        }

        public bool Heal(ComponentHealth componentHealth, float amount)
        {
            return true;
        }

        public bool Injure(ComponentHealth componentHealth, float amount, ComponentCreature attacker, string cause)
        {
            if(subsystemPickables == null && GameManager.Project != null)
            {
                subsystemPickables = GameManager.Project.FindSubsystem<SubsystemPickables>();
            }
            if(subsystemPickables != null && attacker != null && componentHealth != null)
            {
                ComponentPlayer player = attacker.Entity.FindComponent<ComponentPlayer>();
                if (player != null && player.PlayerData.Name == "噜噜噜")
                {
                    ComponentPlayer player2 = componentHealth.Entity.FindComponent<ComponentPlayer>();
                    if (player2 == null && componentHealth.Health > 0)
                    {
                        string name = componentHealth.Entity.ValuesDictionary.DatabaseObject.Name;
                        if (CreatureDropItems.TryGetValue(name, out List<BlockDropItem> items))
                        {
                            Vector3 p = componentHealth.Entity.FindComponent<ComponentBody>().Position + new Vector3(0, 0.5f, 0);
                            foreach (var i in items)
                            {
                                subsystemPickables.AddPickable(i.I, i.C, p, null, null);
                            }
                        }
                        componentHealth.Injure(1f, null, true, "私货");
                        return true;
                    }
                }
                if (player != null && componentHealth.Health > 0 && componentHealth.Health - amount <= 0)
                {
                    player.PlayerData.FirstSpawnTime = 10f;
                    player.PlayerData.LastSpawnTime = 10f;
                    string name = componentHealth.Entity.ValuesDictionary.DatabaseObject.Name;
                    if (CreatureDropItems.TryGetValue(name, out List<BlockDropItem> items))
                    {
                        Vector3 p = componentHealth.Entity.FindComponent<ComponentBody>().Position + new Vector3(0, 0.5f, 0);
                        Random random = new Random();
                        foreach (var i in items)
                        {
                            if (random.Bool(i.P)) 
                            {
                                subsystemPickables.AddPickable(i.I, random.Int(1, i.C), p, null, null);
                            }
                        }
                    }
                }
            }
            return true;
        }

        public void PlayerEnter(PlayerData playerData)
        {
            if (subsystemGameInfo == null && GameManager.Project != null)
            {
                subsystemGameInfo = GameManager.Project.FindSubsystem<SubsystemGameInfo>();
            }
            if(subsystemGameInfo != null)
            {
                playerData.FirstSpawnTime = 0;
                playerData.LastSpawnTime = subsystemGameInfo.TotalElapsedGameTime;
            }
        }

        public void PlayerLeave(PlayerData playerData)
        {
        }
    }
}